﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.DeleteCoverage.CApPaintDeleteCoverageLogic
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Paint.DisplayCoverage;
using EngineeringInternalExtension;
using System.Windows.Forms;
using System.Windows.Input;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.Paint.DeleteCoverage;

public class CApPaintDeleteCoverageLogic
{
  private static CApPaintDeleteCoverageLogic m_instance;
  private bool m_isMeshEmphasizing;

  public static CApPaintDeleteCoverageLogic Instance
  {
    get
    {
      if (CApPaintDeleteCoverageLogic.m_instance == null)
        CApPaintDeleteCoverageLogic.m_instance = new CApPaintDeleteCoverageLogic();
      return CApPaintDeleteCoverageLogic.m_instance;
    }
  }

  private CApPaintDeleteCoverageLogic()
  {
  }

  public bool Enable => this.IsMeshReady && !this.IsSimulationRunning && !this.IsMeshEmphasizing;

  public bool IsMeshReady => TxApplicationEx.PaintManager().StudyHasMeshes;

  public bool IsSimulationRunning
  {
    get => TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning();
  }

  public bool IsMeshEmphasizing
  {
    get => this.m_isMeshEmphasizing;
    set => this.m_isMeshEmphasizing = value;
  }

  public void DeleteCoverage()
  {
    if (!this.HandleBlankMeshesDeletion())
      return;
    this.IsMeshEmphasizing = true;
    this.HandleDeleteCoverage();
    this.IsMeshEmphasizing = false;
  }

  public void HandleDeleteCoverage()
  {
    Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
    Mouse.UpdateCursor();
    TxApplication.ActiveUndoManager.StartTransaction();
    this.RemoveCoverageFromMesh();
    TxApplication.ActiveUndoManager.EndTransaction();
    Mouse.OverrideCursor = (System.Windows.Input.Cursor) null;
    Mouse.UpdateCursor();
    TxApplication.RefreshDisplay();
  }

  public bool HandleBlankMeshesDeletion()
  {
    bool flag = true;
    if (CUiPaintDisplayCoverageUtil.FindBlankMeshesWithCoverage() && this.UserConfirmRequest() == DialogResult.Cancel)
      flag = false;
    return flag;
  }

  private void RemoveCoverageFromMesh()
  {
    TxApplicationEx.PaintManager().RemoveCoverage();
    CApPaintDisplayCoverageManager.Instance.BlankGrid();
  }

  private DialogResult UserConfirmRequest()
  {
    string windowTitle = PaintDisplayCoverageResource.WINDOW_TITLE;
    string meshWithCoverage = PaintDisplayCoverageResource.BLANK_MESH_WITH_COVERAGE;
    MessageBoxButtons messageBoxButtons = MessageBoxButtons.OKCancel;
    MessageBoxIcon messageBoxIcon = MessageBoxIcon.Question;
    return TxMessageBox.Show(meshWithCoverage, windowTitle, messageBoxButtons, messageBoxIcon);
  }
}
